﻿
using System.Collections.Generic;
namespace SanguoSharp.Server
{
    public sealed class KillResponseHandler : ResponseHandler
    {
        private int power;
        private List<Card> 八卦Checked;
        private bool abilityUsed;
        private bool cannotmiss;

        public KillResponseHandler(Player targetPlayer, Player causedBy, int power)
            : base(RequestType.Killed, targetPlayer, causedBy)
        {
            cannotmiss=causedBy.Character.CannotMiss;
            this.power = power;
            八卦Checked = new List<Card>();
            abilityUsed = false;
        }
        public KillResponseHandler(Player targetPlayer, Player causedBy)
            : this(targetPlayer, causedBy, 1)
        {
        }

        private void OnResult(Card card, bool result)
        {
            if (!cannotmiss)
            {
                if (card != null)
                    if (card.Type == CardType.八卦阵)
                        八卦Checked.Add(card);
                if (result)
                {
                    if (card != null && card.Type != CardType.八卦阵)
                        if (card.Type != CardType.闪)
                            Game.GameTable.PlayerRespondWithCard(card, CardType.闪);
                        else
                            Game.GameTable.PlayerRespondWithCard(card);

                    if (--power == 0)
                        End();
                    else
                    {
                        八卦Checked.Clear();
                        abilityUsed = false;
                    }
                }
            }
            else
            {
                RequestedPlayer.ModifyLifePoints(-1, CausedBy);
                End();
            }
        }
        protected override void OnRespondCard(Card card)
        {
            if (card.Owner != RequestedPlayer)
                throw new BadCardException();

            if (八卦Checked.Contains(card))
                throw new BadCardException();

            RequestedPlayer.CheckMissed(card, OnResult);
        }
        protected override void OnRespondUseAbility()
        {
            if (abilityUsed)
                throw new BadUsageException();

            RequestedPlayer.Character.CheckMissed(OnResult);
            abilityUsed = true;
        }
        protected override void OnRespondNoAction()
        {
            RequestedPlayer.ModifyLifePoints(-1, CausedBy);
            End();
        }
    }
}

